package com.example.dormitory_system.controller;

import com.example.dormitory_system.aspect.CheckOneSysUserRole;
import com.example.dormitory_system.aspect.NoCheck;
import com.example.dormitory_system.domain.R;
import com.example.dormitory_system.domain.TableDataInfo;
import com.example.dormitory_system.domain.dto.LoginDTO;
import com.example.dormitory_system.domain.dto.sysuser.*;
import com.example.dormitory_system.domain.dto.tsysuser.ResetPasswordDTO;
import com.example.dormitory_system.domain.vo.sysuser.SysLoginVO;
import com.example.dormitory_system.domain.vo.user.LoginVO;
import com.example.dormitory_system.service.*;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/sys")
@CheckOneSysUserRole
public class OneSystemUserController extends BaseController {
    @Autowired
    private UserService userService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private HolidayStayService holidayStayService;
    @Autowired
    private RoomService roomService;
    @Autowired
    private BuildingService buildingService;

    /**
     * 管理员登入
     * @param loginDTO
     * @return
     */
    @PostMapping("/login")
    @NoCheck
    public R<SysLoginVO> login(@RequestBody LoginDTO loginDTO) {
        return R.ok(userService.login(loginDTO.getUsername(), loginDTO.getPassword()));
    }

    /**
     * 二级管理员注册
     * @param registerDTO
     * @return
     */
    @PostMapping("/register")
    public R<Void> sysRegister(@RequestBody RegisterDTO registerDTO) {
        return toR(userService.register(registerDTO));
    }

    /**
     * 学生注册
     * @param registerDTO
     * @return
     */
    @PostMapping("/stu/register")
    public R<Void> register(@RequestBody RegisterDTO registerDTO) {
        return toR(studentService.register(registerDTO));
    }

    /**
     * 获取管理员需要审核的留校信息
     * @param userId 管理员自己的id
     * @param status 查询状态
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/stay/list")
    public TableDataInfo listHolidayStay(Long userId,Integer status,String name,
                                         String username,@RequestParam (defaultValue = "1") Integer pageNum,@RequestParam (defaultValue = "10") Integer pageSize) {
        return getDataTable(holidayStayService.sysListHolidayStay(userId, status,name,username, pageNum, pageSize));
    }

    /**
     * 更新留校申请
     * @param holidayStayUpdateDTO
     * @return
     */
    @PutMapping("/stay/update")
    public R<Void> updateHolidayStay(@RequestBody HolidayStayUpdateDTO holidayStayUpdateDTO){
        return toR(holidayStayService.updateHolidayStay(holidayStayUpdateDTO));
    }

    /**
     * 更新 换宿舍申请
     * @param changeRoomUpdateDTO
     * @return
     */
    @PutMapping("/room/update")
    public R<Void> updateRoom(@RequestBody ChangeRoomUpdateDTO changeRoomUpdateDTO){
        return toR(roomService.sysUpdateRoom(changeRoomUpdateDTO));
    }


    /**
     * 更新宿舍信息
     * @param roomUpdateDTO
     * @return
     */
    @PutMapping("/room/updateself")
    public R<Void> updateRoom(@RequestBody RoomUpdateDTO roomUpdateDTO){
        return toR(roomService.updateRoom(roomUpdateDTO));
    }


    /**
     * 获取宿舍列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/room/list")
    public TableDataInfo sysListRoom(@RequestParam (defaultValue = "1") Integer pageNum,@RequestParam (defaultValue = "10") Integer pageSize,Long buildingId,String roomName) {
        return getDataTable(roomService.sysListRoom(pageNum,pageSize,buildingId,roomName));
    }


    /**
     * 获取楼栋列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/building/list")
    public TableDataInfo sysListBuilding(@RequestParam (defaultValue = "1") Integer pageNum,@RequestParam (defaultValue = "10") Integer pageSize) {
        return getDataTable(buildingService.sysListBuilding(pageNum,pageSize));
    }
    /**
     * excel导入楼栋
     * @param file
     * @return
     */
    @PostMapping("/building/import")
    public R<Void> importBuildings(@RequestParam("file") MultipartFile file) { //负责人和负责电话不需要导入
        try {
            buildingService.importBuildings(file);
            return R.ok();
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 更新楼栋信息
     * @param buildingDTO
     * @return
     */
    @PutMapping("/building/update")
    public R<Void> updateBuilding(@RequestBody BuildingDTO buildingDTO){
        return toR(buildingService.updateBuilding(buildingDTO));
    }

    /**
     * excel导入宿舍
     * @param file
     * @return
     */
    @PostMapping("/room/import")
    public R<Void> importRooms(@RequestParam("file") MultipartFile file) {
        try {
            roomService.importRooms(file);
            return R.ok();
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * excel导入学生
     * @param file
     * @return
     */
    @PostMapping("/student/import")
    public R<Void> importStudents(@RequestParam("file") MultipartFile file) {
        try {
            studentService.importStudents(file);
            return R.ok();
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 管理员查看换宿舍记录
     * @param status 审核状态
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/changeRoom/list")
    public TableDataInfo listchangeRoom(Long userId,Integer status, String name, String username, @RequestParam (defaultValue = "1") Integer pageNum, @RequestParam (defaultValue = "10") Integer pageSize){
        return getDataTable(roomService.listchangeRoom(status,name,username,pageNum,pageSize,userId));
    }

    @GetMapping("/changeRoom/list2")
    public TableDataInfo listChangeRoom2(Long userId,Integer status, String name, String username, @RequestParam (defaultValue = "1") Integer pageNum, @RequestParam (defaultValue = "10") Integer pageSize){
        return getDataTable(roomService.listChangeRoom2(status,name,username,pageNum,pageSize,userId));
    }

    /**
     * 重置密码
     * @param resetPasswordDTO
     * @return
     */
    @PostMapping("/resetPassword")
    public R<Boolean> resetPassword(@RequestBody ResetPasswordDTO resetPasswordDTO){
        return R.ok(userService.resetpassoword(resetPasswordDTO.getUserId(),resetPasswordDTO.getOldPassword(),resetPasswordDTO.getNewPassword()));
    }

    @DeleteMapping("/delete/user")
    public R<Void> deleteUser(Long userId){
        return toR(userService.deleteUser(userId));
    }

    @GetMapping("/room/findStudent")
    public TableDataInfo findStudent(String roomName,String buildingName){
        return getDataTable(studentService.findStudent(roomName,buildingName));
    }


}